<template>
  <div class="page-root">
    <BasePanel>
      <BaseSearch :search="searchProps" @searchData="updateTable" @resetData="updateTable" />
      <div class="table-top-wrap">
        <div class="table-title">
          <span>列表</span>
        </div>
        <div class="btn-gruop">
          <a-button type="primary" icon="download" ghost @click="handleExport">导出</a-button>
        </div>
      </div>
      <BaseTable :loading="loading" :columns="columns" :dataSource="dataSource" :pagination="false"
        :scroll="{ x: 1000 }">
      </BaseTable>
      <div v-show="dataSource.length > 0" class="page-footer">
        <BasePagination :page-key.sync="pageKey" :get-list="tableInit" />
      </div>
    </BasePanel>
  </div>
</template>

<script>
import { searchProps, columns } from './option.js';
import * as Ajax from '@/api/channel-roi';
import { ajaxDownload } from '@/utils/export.js';
export default {
  data() {
    return {
      searchProps,
      searchForm: {},
      columns,
      loading: true,
      dataSource: [],
      pageKey: {
        pageSize: 10,
        total: 1,
        current: 1,
      },
    };
  },
  created() {
    this.tableInit();
    this.getSelect();
  },
  methods: {
    handleExport() {
      const { current, pageSize } = this.pageKey;
      const params = { pageNum: current, pageSize, ...this.searchForm };
      const queryString = new URLSearchParams(params).toString();  
      ajaxDownload(`/web/report/roi/export?${queryString}`, 'get')(params);
    },
    getSelect() {
      Ajax.getChannelSelect().then((res) => {
        if (res.success) {
          searchProps[0].selectOptions = Array.isArray(res.data)
          // searchProps[1].selectOptions = [this.getFormattedDate('start'),this.getFormattedDate('end')]
          // console.log('只',searchProps)
            ? res.data.map((it) => ({
              ...it,
              label: it.channelName,
              value: it.id,
            }))
            : [];
        }
      });
    },
    // 当天日期
    getFormattedDate(type) {
      const today = new Date();
      const year = today.getFullYear();
      const month = String(today.getMonth() + 1).padStart(2, '0');
      const day = String(today.getDate()).padStart(2, '0');

      if (type === 'start') {
        return `${year}-${month}-${day} 00:00:00`;
      } else if (type === 'end') {
        return `${year}-${month}-${day} 23:59:59`;
      }
    },
    updateTable(params = {}) {
      this.searchForm = { ...params };
      this.pageKey = this.$options.data().pageKey;
      this.tableInit();
    },
    // table 初始化
    tableInit() {
      this.loading = true;
      const { current, pageSize } = this.pageKey;
      const params = { pageNum: current, pageSize, ...this.searchForm };
      if (params.time) {
        params.startTime = params.time[0] + ' 00:00:00';
        params.endTime = params.time[1] + ' 23:59:59';
      } else {
        params.startTime = this.getFormattedDate('start');
        params.endTime = this.getFormattedDate('end');
        // params.startTime = '';
        // params.endTime = '';
      }
      delete params.time;
      Ajax.getRoiList(params)
        .then((res) => {
          const { data, totalCount } = res;
          this.dataSource = Array.isArray(data) ? data : [];
          this.pageKey.total = totalCount;
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.table-wrap {
  margin-top: 16px;
}

.table-top-wrap {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.table-img {
  max-height: 20px;
  max-width: 100px;
}

.table-title {
  flex: 1;

  span {
    line-height: 32px;
    font-weight: bold;
    font-size: 16px;
    display: flex;
    align-items: center;

    &::before {
      content: '';
      display: block;
      width: 3px;
      height: 18px;
      background-color: #1890ff;
      margin-right: 8px;
      position: relative;
      top: -1px;
    }
  }
}

.btn-gruop {
  display: flex;
  justify-content: flex-end;

  &>button {
    margin-left: 12px;

    &:first-child {
      margin-left: 0;
    }
  }
}
</style>
